// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 



using NUnit.Framework;
using SoapTrace.Core;
using SoapTrace.Core.Formatters;
using SoapTrace.Tests.Config;
using System;
using System.IO;
using System.Windows.Forms;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Tests the loading and saving of the soap trace configuration.
	/// </summary>
	[ TestFixture ]
	public class UserConfigTestFixture
	{
		/// <summary>
		/// The config object under test.
		/// </summary>
		SoapTraceConfiguration Config;
		
		/// <summary>
		/// Flag indicating that there was an existing config file holding 
		/// user settings.  This file needs to be preserved and not 
		/// overwritten whilst we are testing.
		/// </summary>
		bool UserConfigExists;
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			Config = new SoapTraceConfiguration( );
			Config.DebugMessagesCount = 111;
			
			// Make a backup of any existing user config file.
			
			string ConfigFilename = 
				Path.Combine( SoapTraceConfiguration.UserConfigurationFolder, SoapTraceConfiguration.UserConfigurationFilename );
			
			UserConfigExists = File.Exists( ConfigFilename );
			
			if( UserConfigExists )
			{
				string DestinationFilename = String.Concat( ConfigFilename, ".backup" );
				File.Move( ConfigFilename, DestinationFilename );
			}
		}
		
		/// <summary>
		/// Cleans up after each test.
		/// </summary>
		[ TearDown ]
		public void TearDown( )
		{
			// Delete user config file we made during the test.
			
			string ConfigFilename = 
				Path.Combine( SoapTraceConfiguration.UserConfigurationFolder, SoapTraceConfiguration.UserConfigurationFilename );
			
			if( File.Exists( ConfigFilename ) )
			{
				File.Delete( ConfigFilename );
			}
			
			// Replace user config file if a backup was made.
			
			if( UserConfigExists )
			{
				File.Move( String.Concat( ConfigFilename, ".backup" ), ConfigFilename );
			}
		}		
		
		/// <summary>
		/// Tests that the <see cref="SoapTraceConfiguration"/>
		/// save the config file to the correct place.
		/// </summary>
		[ Test ]
		public void Save( )
		{
			string ConfigFilename = Path.Combine( SoapTraceConfiguration.UserConfigurationFolder, SoapTraceConfiguration.UserConfigurationFilename );
			                                     
			Assert.IsFalse( File.Exists( ConfigFilename ),
			                "User config file should not exist" );
			
			Config.Save( );
			
			Assert.IsTrue( File.Exists( ConfigFilename ),
			                "User config file should exist" );
		}
		
		/// <summary>
		/// Tests that the user config file is not loaded
		/// since it does not exist and the settings in SoapTrace.exe.config
		/// override everything.
		/// </summary>
		[ Test ]
		public void Load( )
		{
			string ConfigFilename = Path.Combine( SoapTraceConfiguration.UserConfigurationFolder, SoapTraceConfiguration.UserConfigurationFilename );
			                                     
			Assert.IsFalse( File.Exists( ConfigFilename ),
			                "User config file should not exist" );
			
			Config = SoapTraceConfiguration.Load( );
			
			SoapTraceConfiguration ExpectedConfig = TestConfig.GetSoapTraceConfig( );
			
			Assert.AreEqual( ExpectedConfig.DebugMessagesCount,
			                Config.DebugMessagesCount,
			                "DebugMessagesCount in config is incorrect." );
			
			Assert.AreEqual( ExpectedConfig.MessageFormatterEntries.Count,
			                Config.MessageFormatterEntries.Count,
			                "Wrong number of message formatters." );
		}
		
		/// <summary>
		/// Tests that the user settings are read in if available.
		/// </summary>
		[ Test ]
		public void LoadUserSettings( )
		{
			int DebugMessagesCount = 54;
			
			Config.DebugMessagesCount = DebugMessagesCount;
			
			MessageFormatterEntry Entry = new MessageFormatterEntry( );
			Entry.FullTypeName = "Foo.Tests.Formatter.Foo, Foo.Tests";
			
			Config.MessageFormatterEntries.Add( Entry );
			                                   
			Save( );
						
			Config = SoapTraceConfiguration.Load( );
			
			Assert.AreEqual( DebugMessagesCount, 
			                 Config.DebugMessagesCount,
			                "DebugMessagesCount is incorrect." );
			
			Assert.AreEqual( 1, Config.MessageFormatterEntries.Count, 
			                "Did not read in message formatters." );
		}	
	}
}
